package com.jeecg.p3.dzdp.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.VelocityContext;
import org.jeecgframework.p3.base.vo.WeixinDto;
import org.jeecgframework.p3.core.util.plugin.ViewVelocity;
import org.jeecgframework.p3.core.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jeecg.p3.dzdp.entity.WxActDzdpBasic;
import com.jeecg.p3.dzdp.entity.WxActDzdpProperty;
import com.jeecg.p3.dzdp.entity.WxActDzdpRelation;
import com.jeecg.p3.dzdp.entity.WxActDzdpUser;
import com.jeecg.p3.dzdp.service.WxActDzdpBasicService;
import com.jeecg.p3.dzdp.service.WxActDzdpOptService;
import com.jeecg.p3.dzdp.service.WxActDzdpPropertyService;
import com.jeecg.p3.dzdp.service.WxActDzdpRelationService;
import com.jeecg.p3.dzdp.utils.ContextHolderUtils;
/**
 * 描述：定制搭配衣服
 *
 */

@Controller
@RequestMapping("/dzdp")
public class DzdpController extends BaseController {
	  @Autowired
	  private WxActDzdpPropertyService wxActDzdpPropertyService;
	  @Autowired
	  private WxActDzdpBasicService wxActDzdpBasicService;
	  @Autowired
	  private WxActDzdpOptService wxActDzdpOptService;
	  @Autowired
	  private WxActDzdpRelationService wxActDzdpRelationService;
	/**
	 * 跳转活动页面
	 * @throws Exception 
	 */
	@RequestMapping(value="toIndex",method={RequestMethod.GET,RequestMethod.POST})
	public void toIndex(@ModelAttribute WeixinDto weixinDto,HttpServletRequest request, HttpServletResponse response,
			ModelMap model) throws Exception{
		LOG.info(request, "toIndex parameter WeixinDto={}.",new Object[] { weixinDto });
		
		VelocityContext velocityContext = new VelocityContext();
		String viewName = "dzdp/default/index.vm";
		ViewVelocity.view(request,response,viewName,velocityContext);
	}
	
	/**
	 * 跳转活动页面
	 * @throws Exception 
	 */
	@RequestMapping(value="toStep",method={RequestMethod.GET,RequestMethod.POST})
	public void toStep(@ModelAttribute WeixinDto weixinDto,HttpServletRequest request, HttpServletResponse response,
			ModelMap model) {
		LOG.info(request, "toIndex parameter WeixinDto={}.",new Object[] { weixinDto });
		try{
		String jwid = weixinDto.getJwid();
		List<WxActDzdpBasic> basicList=wxActDzdpBasicService.queryBasics(jwid);
		
		String toUrl=(String)request.getParameter("toUrl");
		String viewName = "dzdp/default/"+toUrl+".vm";
		VelocityContext velocityContext = new VelocityContext();
		if(toUrl!=null){
		String toStep=toUrl.split("_")[1];
		int toStepInt=Integer.parseInt(toStep);
		WxActDzdpBasic wxActDzdpBasic=basicList.get(toStepInt-1);
		List<WxActDzdpProperty> propertyList=wxActDzdpPropertyService.queryPropertyByBaseId(wxActDzdpBasic.getId());
		velocityContext.put("DzdpBasic", wxActDzdpBasic);
		velocityContext.put("propertyList", propertyList);
		ViewVelocity.view(request,response,viewName,velocityContext);
		}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 定制搭配报告
	 */
	@RequestMapping(value="toReport",method={RequestMethod.GET,RequestMethod.POST})
	public void toReport(@ModelAttribute WxActDzdpUser wxActDzdpUser,HttpServletRequest request, HttpServletResponse response,
			ModelMap model){
		LOG.info(request, "toIndex parameter WeixinDto={}.",new Object[] { wxActDzdpUser });
		VelocityContext velocityContext = new VelocityContext();
		try{
		velocityContext.put("age", wxActDzdpUser.getAge());
		velocityContext.put("height",wxActDzdpUser.getHeight());
		velocityContext.put("weight",wxActDzdpUser.getWeight());
		//身形
		WxActDzdpProperty figurePro=wxActDzdpPropertyService.queryById(wxActDzdpUser.getFigure());
		List<WxActDzdpRelation> figureOptsList=wxActDzdpRelationService.queryByPropertyId(figurePro.getId());
		figurePro.setOptsList(figureOptsList);
		velocityContext.put("figure",figurePro);
		//脸型
		WxActDzdpProperty facePro=wxActDzdpPropertyService.queryById(wxActDzdpUser.getFace());
		List<WxActDzdpRelation> faceOptsList=wxActDzdpRelationService.queryByPropertyId(facePro.getId());
		facePro.setOptsList(faceOptsList);
		velocityContext.put("face",facePro);
		//肤色
		WxActDzdpProperty skinPro=wxActDzdpPropertyService.queryById(wxActDzdpUser.getSkin());
		List<WxActDzdpRelation> skinOptsList=wxActDzdpRelationService.queryByPropertyId(skinPro.getId());
		skinPro.setOptsList(skinOptsList);
		velocityContext.put("skin",skinPro);
		//问题
		List<WxActDzdpProperty> problemPropertyList=null;
		if(wxActDzdpUser.getProblems()!=null){
			problemPropertyList=wxActDzdpPropertyService.queryPropertyInIds(wxActDzdpUser.getProblems());//获取所有问题
			for(WxActDzdpProperty pro:problemPropertyList){
				List<WxActDzdpRelation> dzdpRelationList=wxActDzdpRelationService.queryByPropertyId(pro.getId());//每个问题的意见
				pro.setOptsList(dzdpRelationList);
			}
		}
		velocityContext.put("problems",problemPropertyList);
		
		String viewName = "dzdp/default/report.vm";
		ViewVelocity.view(request,response,viewName,velocityContext);
		}catch(Exception e){
			e.printStackTrace();
		}
		

	}	
}
